
local LoginNoticeView = class("LoginNoticeView",function()
    return display.newModalDialog()
end)

local originWidth = 0
local originHeight = 0

function LoginNoticeView:ctor()
    self._noticeCount = 1 -- 第几条公告

    local view, bindingNodes = cc.CSLoader:createNode(Res.LoginNoticeScene,true, true, true)
    view:setContentSize(display.width,display.height)
    ccui.Helper:doLayout(view)
    self:addChild(view)

    self._ui = display.uiDelegate(view)
    self.announcementList = self._ui.notelite
    originWidth, originHeight = self.announcementList:getContentSize2()
    self.announcementList:removeAllItems()

    self._ui.Button_back:addClickEventListener(function()
        if self._noticeCount > #self._notices then
            self:removeFromParent()
            executeFunc(self._onClose)
        else
            self:refreshEachNotice()
        end
    end)

    self:setNodeEventEnabled(true)
end

function LoginNoticeView:onExit()
    executeFunc(self._onClose)
end

function LoginNoticeView:setData(data)
    -- dump(data, "LoginNoticeView:setData：")

    self._notices = data
    self._noticeCount = 1
    self:refreshEachNotice()
end

function LoginNoticeView:addNotice(data)
    if self._notices then
        self._notices[#self._notices + 1] = data
    end
end

-- data = message Notice{
--     string title = 1;
--     int32 titleType = 2; // 内容类型（名字取的不好） 0文本，1 url图片，2 本地pic地址
--     string content = 3;
--     int32 priority = 4; // 公告优先级，值越小优先级越高
--     string buttonName = 5; // 按钮名字 为空则不显示
--     string buttonUrl = 6; // 点击按钮跳转url，如果按钮名字不为空
-- }
function LoginNoticeView:refreshEachNotice()
    local contentLayout = nil
    self.announcementList:removeAllItems()
    if self._urlImage then
        self._urlImage:removeFromParent()
        self._urlImage = nil
    end

    -- 找下一个notice，找不到关闭界面
    local dataTemp = self._notices[self._noticeCount]
    while not dataTemp do
        if self._noticeCount < #self._notices then
            self._noticeCount = self._noticeCount + 1
            dataTemp = self._notices[self._noticeCount]
        else
            break
        end
    end
    if not dataTemp then
        self:removeFromParent()
        executeFunc(self._onClose)
        return 
    end

    dataTemp.content = string.urldecode(dataTemp.content)

    self._ui.Text_name:setString( dataTemp.title ) -- 标题
    self._ui.img:setVisible( false )
    if 0 == dataTemp.titleType then -- 统一由富文本处理，文本中可能带有url指向的pic
        local richTextContent = ""
        -- if "" ~= dataTemp.buttonUrl then
        --     -- if string.find(dataTemp.content, "http") then -- 内容带url、则认为内容只能是一个url链接
        --     --     richTextContent = display.getRichTextUrlXml( display.getRichTextUrlImgXml( dataTemp.content, originWidth, originHeight ), dataTemp.buttonUrl )
        --     -- else
        --     --     richTextContent = display.getRichTextUrlXml( dataTemp.content, dataTemp.buttonUrl )
        --     -- end
        --     richTextContent = display.getRichTextUrlXml( dataTemp.content, dataTemp.buttonUrl )
        -- else
        --     richTextContent = dataTemp.content
        -- end
        richTextContent = dataTemp.content
        contentLayout = self:createRichTextContent( richTextContent )
    elseif 1 == dataTemp.titleType then -- 网络url
        --self._ui.Text_name:setString( "" )

        self._urlImage = ccui.ImageView:createWithUrl( dataTemp.content )
        self._urlImage:setAnchorPoint( self._ui.img:getAnchorPoint() )
        self._urlImage:setContentSize( self._ui.img:getContentSize() )
        self._urlImage:setPosition( self._ui.img:getPosition() )

        if "" ~= dataTemp.buttonUrl then
            self._urlImage:setTouchEnabled( true )
            display.uiAddClick( self._urlImage, function()
                if self._onOpenUrl then
                    self._onOpenUrl( dataTemp.buttonUrl )
                end
            end )
        end

        self._ui.img:getParent():addChild( self._urlImage )
    elseif 2 == dataTemp.titleType then -- 本地图片地址
        self._ui.img:setVisible(true)
        self._ui.img:loadTexture( dataTemp.content )

-------------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>> test
    elseif -1 == dataTemp.titleType then -- MSDK临时数据处理
        self._ui.Text_name:setString("")
        local richTextContent = ""
        if "" ~= dataTemp.buttonUrl then
            richTextContent = display.getRichTextUrlXml(dataTemp.title, dataTemp.buttonUrl)
        else
            local sizeTemp = self.announcementList:getContentSize()

            local _, r2 = string.find( dataTemp.title, "png'" )
            local m = (r2 + 1)

            local preStrTemp = string.sub(dataTemp.title, 1, m-1)
            local nextStrTemp = string.sub(dataTemp.title, m+1, string.len(dataTemp.title))

            local strTemp = preStrTemp .. " width=" .. "'" .. sizeTemp.width .. "' "  .. "height=" .. "'" .. sizeTemp.height .. "' " .. nextStrTemp
            richTextContent = strTemp
        end
        contentLayout = self:createRichTextContent( richTextContent )
--------------------------------------------------------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< test
    else
        -- dump(dataTemp, "refreshEachNotice:未知类型数据:")
    end

    if contentLayout then
        self.announcementList:pushBackCustomItem( contentLayout )

        self.announcementList:scrollToTop( 0.1, true )
        self.announcementList:setBounceEnabled( true )
        self.announcementList:setGravity( ccui.ListViewGravity.centerVertical )
        self.announcementList:setClippingEnabled( true )
        self.announcementList:forceDoLayout()
        self.announcementList:refreshView()
    end
    self._noticeCount = self._noticeCount + 1
end

function LoginNoticeView:createRichTextContent( str, defaults )
    local frameSize = self.announcementList:getContentSize()
    local contentLayout = ccui.Layout:create()
    -- local contentLayout = cc.LayerColor:create( cc.c4b( 255, 255, 255, 0) )
    contentLayout:setAnchorPoint( cc.p( 0.5, 0.5 ) )
    contentLayout:setTouchSwallowEnabled( false )
    contentLayout:setTouchEnabled( false )

    local defaults = defaults or {
        -- KEY_FONT_FACE = Res.font,
        KEY_FONT_SIZE = app:isLanguageEN() and 18 or 32,
        KEY_FONT_COLOR_STRING = "#FFFFFF",
    }
    local richText = ccui.RichText:createWithXML( str, defaults )
    richText:ignoreContentAdaptWithSize( false )
    -- richText:setWrapMode( 1 )
    richText:setContentSize( cc.size( frameSize.width, 0 ) )
    richText:formatText()
    local f_size = richText:getFormatSize()
    richText:setPosition( frameSize.width * 0.5, 0 )
    richText:setOpenUrlHandler(function(sender, url)
        if self._onOpenUrl then
            self._onOpenUrl(url)
        end
    end)

    contentLayout:setContentSize( cc.size( frameSize.width, f_size.height ) )
    contentLayout:addChild( richText )
    contentLayout:setPosition( cc.p( 0, frameSize.height - f_size.height ) )

    return contentLayout
end

function LoginNoticeView:setCloseDelegate(cb)
    self._onClose = cb
end

function LoginNoticeView:setOpenUrlDelegate(cb)
    self._onOpenUrl = cb
end


function LoginNoticeView:setClostBtnVisible(trueOrFalse)
    self._ui.Button_back:setVisible(trueOrFalse)
end
   

return LoginNoticeView
